<template>
  <div>
    <div class="nav">
      <van-nav-bar
        title="特惠物流"
        @click-left="onClickLeft"
        left-text="返回"
        left-arrow
      >
        <template #right> 设置 </template>
      </van-nav-bar>
    </div>
    <div class="main" ref="wrapper">
      <div>
        <div style="height: 1.5rem"></div>
        <div class="item" v-for="(item, index) in actives" :key="index">
          <div class="item_top">
            <img
              src="https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/tuijina_icon.png"
              alt=""
            />
            <p>每日推荐</p>
          </div>
          <div class="item_content">
            <p class="title">{{ item.title }}</p>
            <div class="item_content_c">
              <img :src="iconUrl + item.Pic" />
              <p class="context">
                {{ item.content }}
              </p>
            </div>
            <div class="time">{{ changetime(item.time) }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// const ws = new WebSocket("ws://localhost:8060");
const ws = new WebSocket("ws://106.55.48.147:8060");
import myfun from "../../utils/timeDate.js";
import BScroll from "@better-scroll/core";
import { mapState } from "vuex";
export default {
  data() {
    return {
      actives: [],
    };
  },
  mounted() {
    this.initBScroll();
    this.getLogisticsInformation();
    ws.addEventListener("open", this.handleWsOpen.bind(this), false);
    ws.addEventListener("close", this.handleWsClose.bind(this), false);
    ws.addEventListener("error", this.handleWsError.bind(this), false);
    ws.addEventListener("message", this.handleWsMessage.bind(this), false); //介绍信息对策方法
  },
  computed: {
    ...mapState(["emailLogin"]),
  },
  watch: {
    actives() {
      this.$nextTick(() => {
        this.scroll.refresh();
      });
    },
  },
  methods: {
    onClickLeft() {
      this.$router.push("/Message");
    },
    initBScroll() {
      this.scroll = new BScroll(this.$refs.wrapper, {
        mouseWheel: true,
        click: true,
        tap: true,
      });
    },
    refresh() {
      this.scroll && this.scroll.refresh();
    },
    async getLogisticsInformation() {
      const { data: res } = await this.$http.get("GetActivityPush");
      this.actives = res.data;
      console.log(res);
    },
    changetime(t) {
      return myfun.dateTime1(t);
    },
    handleWsOpen(e) {
      console.log("连接成功：", e);
      this.getUid();
    },
    handleWsClose(e) {
      console.log("连接关闭：", e);
    },
    handleWsError(e) {
      console.log("连接错误：", e);
    },
    //接收消息
    handleWsMessage(e) {
      let data = JSON.parse(e.data);
      if (data.msgType == 14) {
        this.getLogisticsInformation();
      }
      console.log(data);
    },
    getUid() {
      ws.send(JSON.stringify({ uid: this.emailLogin[0].userID }));
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  position: absolute;
  overflow: hidden;
  width: 100%;
  top: 20px;
  left: 0;
  right: 0;
  bottom: 0;
  background: #f7f7f7;
  .item {
    width: 360px;
    margin: 10px auto;
    background: white;
    border-radius: 10px;
    .item_top {
      display: flex;
      margin-left: 10px;
      width: 95px;
      justify-content: space-between;
      align-items: center;
      padding: 5px 0;
      img {
        width: 35px;
        height: 35px;
      }
      p {
        color: #a1a0a0;
      }
    }
    .item_content {
      margin-left: 45px;
      .title {
        font-size: 16px;
        padding-bottom: 10px;
        color: #3d3d3d;
      }
      .item_content_c {
        display: flex;
        /* justify-content: space-around; */
        width: 306px;
        align-items: center;
        background: #f8f8f8;
        height: 60px;
        border-radius: 10px;
        img {
          width: 60px;
          height: 60px;
          border-radius: 10px;
        }
        .context {
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          text-overflow: ellipsis;
          /* width: 208px; */
          color: #5a5959;
          /* font-size: 15px; */
          margin-left: 10px;
        }
      }
      .time {
        padding: 10px 0;
        color: #767676;
      }
    }
  }
}
</style>